//
//  NewsViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NewsDetailViewController.h"
#import "NewsTalkViewController.h"

@interface NewsViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation NewsViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewsTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"NewsTableViewCell"];
    [self creatDataList];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        [dic setValue:@"0" forKey:@"type"]; //0:学生列表   1老师列表
    }else{
        [dic setValue:@"1" forKey:@"type"]; //0:学生列表   1老师列表，家长列表
    }
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppMessageGetNoticeContactList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];
            [NewsInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                return @{@"newsId" : @"id"};
            }];
            NSMutableArray *mutaArray = [[NSMutableArray alloc]init];
            NSMutableDictionary *sysMessDic = [[NSMutableDictionary alloc]initWithDictionary:response[@"systemMessage"]];
            [sysMessDic setValue:@"通知" forKey:@"name"];
            [sysMessDic setValue:@"icon_system_message" forKey:@"icon"];
            [sysMessDic setValue:response[@"systemMessage"][@"add_time"] forKey:@"update_time"];
            [mutaArray addObject:[NewsInfo mj_objectWithKeyValues:sysMessDic]];
            if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) { //学生
                NSMutableDictionary *sysMessDic = [[NSMutableDictionary alloc]initWithDictionary:response[@"friendMessage"]];
                [sysMessDic setValue:@"评论和赞" forKey:@"name"];
                [sysMessDic setValue:@"icon_other_message" forKey:@"icon"];
                [sysMessDic setValue:response[@"friendMessage"][@"add_time"] forKey:@"update_time"];
                [mutaArray addObject:[NewsInfo mj_objectWithKeyValues:sysMessDic]];
            }
            [self.dataArray addObject:[NewsInfo mj_objectArrayWithKeyValuesArray:mutaArray]];
            
            [self.dataArray addObject:[NewsInfo mj_objectArrayWithKeyValuesArray:response[@"noticeList"]]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.newsInfo = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsInfo *newsInfo = self.dataArray[indexPath.section][indexPath.row];
    if (indexPath.section) {
        NewsTalkViewController *newsTalkVC = [[NewsTalkViewController alloc]init];
        newsTalkVC.scanNewsTalkBlock = ^{
            [self creatDataList];
        };
        newsTalkVC.newsId = newsInfo.newsId;
        newsTalkVC.friendId = newsInfo.friend_id;
        [self.navigationController pushViewController:newsTalkVC animated:YES];
    }else{
        NewsDetailViewController *newsDetailVC = [[NewsDetailViewController alloc]init];
        newsDetailVC.scanNewsDetailBlock = ^{
            [self creatDataList];
        };
        newsDetailVC.titleStr = newsInfo.name;
        newsDetailVC.newsId = newsInfo.newsId;
        newsDetailVC.newsType = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 2];
        [self.navigationController pushViewController:newsDetailVC animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 10);
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
